
export function listToTree(data, pid, idKey, pidKey) {
  let nodes = []
  pid = pid || '0'
  idKey = idKey || 'id'
  pidKey = pidKey || 'pid'
  data.forEach(function (item) {
    if (item[pidKey] === pid) {
      let node = item
      node.children = listToTree(data, item[idKey], idKey, pidKey)
      if (node.children.length === 0) {
        delete node.children
      }
      nodes.push(node)
    }
  })
  return nodes
}

//防抖函数
export function debounce(func, wait) {
  let timeout
  return function () {
    let context = this
    clearTimeout(timeout)
    timeout = setTimeout(() => {
      func.apply(context, arguments)
    }, wait)
  }
}

//节流函数
export function throttle(func, wait) {
  let timeout
  return function () {
    let context = this
    if (!timeout) {
      timeout = setTimeout(() => {
        timeout = null
        func.apply(context, arguments)
      }, wait)
    }
  }
}
